<?php

namespace app\api\model\marketing;

use app\api\model\Models;
use think\db\Query;

class Coupons extends Models
{
    /*
     * 定义全局的查询范围
     * @var array
     */
    protected $globalScope = ['noDisplay'];

    /**
     * 设置全局不显示字段
     * @param Query $query
     * @return void
     */
    public function scopeNoDisplay( Query $query )
    {
        $query->field('create_at,update_at', true);
    }

    /**
     * 设置开始时间获取器
     * @return string
     */
    protected function getStartDateTextAttr( $value, $data )
    {
        return date("Y.m.d", strtotime($data['start_date']));
    }

    /**
     * 设置结束时间获取器
     * @return string
     */
    protected function getEndDateTextAttr( $value, $data )
    {
        return date("Y.m.d", strtotime($data['end_date']));
    }

    /**
     * 设置结束时间获取器
     * @return string
     */
    protected function getTypeTextAttr( $value, $data )
    {
        $type = explode(',', $data['type']);
        $config = config('field.coupons.type');
        $rest = array_map(function($v)use($config){
            return $config[$v];
        }, $type);
        return implode(',', $rest);
    }

}
